package entities

import "mlgmd.com/hearsay/base"

type Hearsay struct {
	Id                int   `xorm:"pk autoincr" json:"id"`
	CreatorId         int   `xorm:"index(creator)" json:"creatorId"`
	CreatedTime       int64 `json:"createdTime"`
	DeletedTime       int64 `json:"deletedTime"`
	HearsayContent    `xorm:"extends"`
	RewardId          int    `json:"rewardId"` // 当作为投稿时，引用悬赏
	RewardContent     string `json:"rewardContent"`
	Weight            int    `json:"weight"`
	WeightBase        int    `json:"weightBase"`
	BrowseCount       int    `json:"browseCount"`
	UnlockCount       int    `json:"unlockCount"`
	FavorCount        int    `json:"favorCount"`
	NegateCount       int    `json:"negateCount"`
	CommentCount      int    `json:"commentCount"`
	LastCommentUserId int    `json:"lastCommentUserId"`
	LastCommentTime   int64  `json:"lastCommentTime"`
	Notice            string `xorm:"text" json:"notice"` // 管理员:提醒
	Blocked           bool   `json:"blocked"`            // 管理员:已屏蔽
}

func (h *Hearsay) Mask() {
	h.ProtectedContent = ""
	h.ProtectedImages = []string{}
}

func (h *Hearsay) AfterLoad() {
	if h.PublicImages == nil {
		h.PublicImages = make([]string, 0)
	}
	if h.ProtectedImages == nil {
		h.ProtectedImages = make([]string, 0)
	}
}

type HearsayContent struct {
	PublicContent    string   `xorm:"text" json:"publicContent"`
	PublicImages     []string `json:"publicImages"`
	ProtectedContent string   `xorm:"text" json:"protectedContent"`
	ProtectedImages  []string `json:"protectedImages"`
	Coin             int      `json:"coin"`
}

type HearsayUnlock struct {
	Id          int   `xorm:"pk autoincr" json:"id"`
	HearsayId   int   `xorm:"index(hearsay-user)" json:"hearsayId"`
	UserId      int   `xorm:"index(hearsay-user)" json:"userId"`
	RewardId    int   `json:"rewardId"`                     // 当在悬赏中解锁时，使用的是悬赏的铜币，如果差评，则退回悬赏中
	Result      int   `json:"result"`                       // 结果:0,未定;1,好评;2,差评
	FrozenCoin  int   `xorm:"default(0)" json:"frozenCoin"` // 长时间不评价会慢慢冻结铜板，评价后会释放
	CreatedTime int64 `json:"createdTime"`
	SettleTime  int64 `json:"settleTime"`
}

func (hu HearsayUnlock) Key(name string) int {
	switch name {
	case "user":
		return hu.UserId
	default:
		return hu.Id
	}
}

type HearsayComment struct {
	Comment `xorm:"extends"`
}

type HearsayBrowse struct {
	Id          int   `xorm:"pk autoincr" json:"id"`
	HearsayId   int   `xorm:"index(hearsay-user)" json:"hearsayId"`
	UserId      int   `xorm:"index(hearsay-user)" json:"userId"`
	CreatedTime int64 `json:"createdTime"`
}

// HearsayFilter 从推荐系统中过滤掉
type HearsayFilter struct {
	Id          int   `xorm:"pk autoincr" json:"id"`
	HearsayId   int   `xorm:"index(hearsay-user)" json:"hearsayId"`
	UserId      int   `xorm:"index(hearsay-user)" json:"userId"`
	Seq         int   `json:"seq"`       // 每次刷新列表时重置,用来记录每次推荐的内容
	Confirmed   bool  `json:"confirmed"` // 每次拉取数据只有前几条会被标记为确认的，后面几条需要等下次拉取时确认，已确认的数据不会出现在推荐中
	CreatedTime int64 `json:"createdTime"`
}

func init() {
	base.Must(base.DB.Sync(&Hearsay{}, &HearsayUnlock{}, &HearsayComment{}, &HearsayBrowse{}, &HearsayFilter{}))
}
